package com.cskaoyan.utils.ad;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.entity.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.CouponMapper;

import java.util.List;

public class TransUtils {

    public static AdItems Ad2Vo(Ad ad){
        AdItems adItems = new AdItems();
        adItems.setId(ad.getId());
        adItems.setName(ad.getName());
        adItems.setLink(ad.getLink());
        adItems.setUrl(ad.getUrl());
        adItems.setPosition(ad.getPosition());
        adItems.setContent(ad.getContent());
        adItems.setEnabled(ad.getEnabled());
        adItems.setAddTime(ad.getAddTime());
        adItems.setUpdateTime(ad.getUpdateTime());
        adItems.setDeleted(ad.getDeleted());
        return adItems;
    }


    public static Ad Bo2Ad(AdBo adBo) {
        Ad ad = new Ad();
        ad.setId(adBo.getId());
        ad.setName(adBo.getName());
        ad.setLink(adBo.getLink());
        ad.setUrl(adBo.getUrl());
        ad.setPosition(adBo.getPosition());
        ad.setContent(adBo.getContent());
        ad.setEnabled(adBo.getEnabled());
        ad.setAddTime(adBo.getAddTime());
        ad.setUpdateTime(adBo.getUpdateTime());
        ad.setDeleted(adBo.getDeleted());
        return ad;
    }

    public static CuItems Coupon2CouponVo(Coupon coupon) {
        CuItems cuItems = new CuItems();
        cuItems.setId(coupon.getId());
        cuItems.setName(coupon.getName());
        cuItems.setDesc(coupon.getDesc());
        cuItems.setTag(coupon.getTag());
        cuItems.setTotal(coupon.getTotal());
        cuItems.setDiscount(coupon.getDiscount());
        cuItems.setMin(coupon.getMin());
        cuItems.setLimit(coupon.getLimit());
        cuItems.setType(coupon.getType());
        cuItems.setStatus(coupon.getStatus());
        cuItems.setGoodsType(coupon.getGoodsType());
        cuItems.setGoodsValue(coupon.getGoodsValue());
        cuItems.setTimeType(coupon.getTimeType());
        cuItems.setDays(coupon.getDays());
        cuItems.setAddTime(coupon.getAddTime());
        cuItems.setUpdateTime(coupon.getUpdateTime());
        cuItems.setDeleted(coupon.getDeleted());
        return cuItems;
    }

    public static Coupon CouponBo2Coupon(CouponBo couponBo, CouponMapper couponMapper) {
        Coupon coupon = new Coupon();
        coupon.setId(couponBo.getId());
        coupon.setName(couponBo.getName());
        coupon.setDesc(couponBo.getDesc());
        coupon.setTag(couponBo.getTag());
        coupon.setTotal(couponBo.getTotal());
        coupon.setDiscount(couponBo.getDiscount());
        coupon.setMin(couponBo.getMin());
        coupon.setLimit(couponBo.getLimit());
        coupon.setType(couponBo.getType());
        coupon.setStatus(couponBo.getStatus());
        coupon.setGoodsType(couponBo.getGoodsType());
        coupon.setGoodsValue(couponBo.getGoodsValue());
        //优惠券兑换码模式
        if(coupon.getType() == 2){
            CouponExample couponExample = new CouponExample();
            CouponExample.Criteria criteria = couponExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<Coupon> couponList = couponMapper.selectByExample(couponExample);
            while(true) {
                //随机生成数字大小写字母混排兑换码
                String random = RandomUtils.createBigSmallStrOrRandomNum();
                for (Coupon coupon1 : couponList) {
                   if(random.equals(coupon1.getCode())) break;
                }
                coupon.setCode(random);
                break;
            }
        }else {
            //非兑换券模式
            coupon.setCode(null);
        }
        coupon.setTimeType(couponBo.getTimeType());
        coupon.setDays(couponBo.getDays());
        coupon.setAddTime(couponBo.getAddTime());
        coupon.setUpdateTime(couponBo.getUpdateTime());
        coupon.setDeleted(couponBo.getDeleted());
        return coupon;
    }

    public static CouponVo CouponToCouponVo(Coupon coupon) {
        CouponVo couponVo = new CouponVo();
        couponVo.setId(coupon.getId());
        couponVo.setName(coupon.getName());
        couponVo.setDesc(coupon.getDesc());
        couponVo.setTag(coupon.getTag());
        couponVo.setTotal(coupon.getTotal());
        couponVo.setDiscount(coupon.getDiscount());
        couponVo.setMin(coupon.getMin());
        couponVo.setLimit(coupon.getLimit());
        couponVo.setType(coupon.getType());
        couponVo.setStatus(coupon.getStatus());
        couponVo.setGoodsType(coupon.getGoodsType());
        couponVo.setGoodsValue(coupon.getGoodsValue());
        couponVo.setTimeType(coupon.getTimeType());
        couponVo.setDays(coupon.getDays());
        couponVo.setStartTime(coupon.getStartTime());
        couponVo.setEndTime(coupon.getEndTime());
        couponVo.setAddTime(coupon.getAddTime());
        couponVo.setUpdateTime(coupon.getUpdateTime());
        couponVo.setDeleted(coupon.getDeleted());
        return couponVo;
    }

    public static CouponUserVo CouponUser2CouponUserVo(CouponUser couponUser) {
        CouponUserVo couponUserVo = new CouponUserVo();
        couponUserVo.setId(couponUser.getId());
        couponUserVo.setUserId(couponUser.getUserId());
        couponUserVo.setCouponId(couponUser.getCouponId());
        couponUserVo.setStatus(couponUser.getStatus());
        couponUserVo.setStartTime(couponUser.getStartTime());
        couponUserVo.setEndTime(couponUser.getEndTime());
        couponUserVo.setAddTime(couponUser.getAddTime());
        couponUserVo.setUpdateTime(couponUser.getUpdateTime());
        couponUserVo.setDeleted(couponUser.getDeleted());
        return couponUserVo;
    }

    public static TopicVo topic2topicVo(Topic topic) {
        TopicVo topicVo = new TopicVo();
        topicVo.setId(topic.getId());
        topicVo.setTitle(topic.getTitle());
        topicVo.setSubtitle(topic.getSubtitle());
        topicVo.setPrice(topic.getPrice());
        topicVo.setReadCount(topic.getReadCount());
        topicVo.setPicUrl(topic.getPicUrl());
        topicVo.setSortOrder(topic.getSortOrder());
        topicVo.setGoods(topic.getGoods());
        topicVo.setAddTime(topic.getAddTime());
        topicVo.setUpdateTime(topic.getUpdateTime());
        topicVo.setDeleted(topic.getDeleted());
        topicVo.setContent(topic.getContent());
        return topicVo;
    }

    public static Topic topicBo2topic(TopicBo topicBo) {
        Topic topic = new Topic();
        topic.setId(topicBo.getId());
        topic.setTitle(topicBo.getTitle());
        topic.setSubtitle(topicBo.getSubtitle());
        topic.setPrice(topicBo.getPrice());
        topic.setReadCount(topicBo.getReadCount());
        topic.setPicUrl(topicBo.getPicUrl());
        topic.setSortOrder(topicBo.getSortOrder());
        topic.setGoods(topicBo.getGoods());
        topic.setAddTime(topicBo.getAddTime());
        topic.setUpdateTime(topicBo.getUpdateTime());
        topic.setDeleted(false);
        topic.setContent(topicBo.getContent());
        return topic;
    }

    public static Coupon CouponBoToCoupon(AddCouponBo addCouponBo, CouponMapper couponMapper) {
        Coupon coupon = new Coupon();
        coupon.setName(addCouponBo.getName());
        coupon.setDesc(addCouponBo.getDesc());
        coupon.setTag(addCouponBo.getTag());
        coupon.setTotal(addCouponBo.getTotal());
        coupon.setDiscount(addCouponBo.getDiscount());
        coupon.setMin(addCouponBo.getMin());
        coupon.setLimit(addCouponBo.getLimit());
        coupon.setType(addCouponBo.getType());
        coupon.setStatus(addCouponBo.getStatus());
        coupon.setGoodsType(addCouponBo.getGoodsType());
        coupon.setGoodsValue(addCouponBo.getGoodsValue());
        //优惠券兑换码模式
        if(coupon.getType() == 2){
            CouponExample couponExample = new CouponExample();
            CouponExample.Criteria criteria = couponExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<Coupon> couponList = couponMapper.selectByExample(couponExample);
            while(true) {
                //随机生成数字大小写字母混排兑换码
                String random = RandomUtils.createBigSmallStrOrRandomNum();
                for (Coupon coupon1 : couponList) {
                    if(random.equals(coupon1.getCode())) break;
                }
                coupon.setCode(random);
                break;
            }
        }else {
            //非兑换券模式
            coupon.setCode(null);
        }
        coupon.setTimeType(addCouponBo.getTimeType());
        coupon.setDays(addCouponBo.getDays());
        return coupon;
    }

    public static Coupon CouponBoTOCoupon(UpdateCouponBo updateCouponBo, CouponMapper couponMapper) {
        Coupon coupon = new Coupon();
        coupon.setId(updateCouponBo.getId());
        coupon.setName(updateCouponBo.getName());
        coupon.setDesc(updateCouponBo.getDesc());
        coupon.setTag(updateCouponBo.getTag());
        coupon.setTotal(updateCouponBo.getTotal());
        coupon.setDiscount(updateCouponBo.getDiscount());
        coupon.setMin(updateCouponBo.getMin());
        coupon.setLimit(updateCouponBo.getLimit());
        coupon.setType(updateCouponBo.getType());
        coupon.setStatus(updateCouponBo.getStatus());
        coupon.setGoodsType(updateCouponBo.getGoodsType());
        coupon.setGoodsValue(updateCouponBo.getGoodsValue());
        //优惠券兑换码模式
        if(coupon.getType() == 2){
            CouponExample couponExample = new CouponExample();
            CouponExample.Criteria criteria = couponExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<Coupon> couponList = couponMapper.selectByExample(couponExample);
            while(true) {
                //随机生成数字大小写字母混排兑换码
                String random = RandomUtils.createBigSmallStrOrRandomNum();
                for (Coupon coupon1 : couponList) {
                    if(random.equals(coupon1.getCode())) break;
                }
                coupon.setCode(random);
                break;
            }
        }else {
            //非兑换券模式
            coupon.setCode(null);
        }
        coupon.setTimeType(updateCouponBo.getTimeType());
        coupon.setDays(updateCouponBo.getDays());
        return coupon;
    }
}
